package com.example.mongo.controller;

import com.example.mongo.Model.bean.User;
import com.example.mongo.Model.dto.ResponseData;
import com.example.mongo.Model.dto.UserRegisterDto;
import com.example.mongo.dao.UserMapper;
import com.example.mongo.service.UserService;
import com.example.mongo.utils.CookieUtils;
import com.example.mongo.utils.MD5Util;
import com.example.mongo.utils.RedisUtil;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ResponseData responseData;

    //    /test/updatePassword?userName=12312&userPasswword=ming
    @ResponseBody
    @RequestMapping(value = "/updatePassword",produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String reSetPassword(@RequestParam String userName, @RequestParam String userPasswword) {

        User user = userService.getUserByUserName(userName);
        user.setUserPassword(userPasswword);
        user = userService.updatePassword(user);
        if (null==user){
            responseData.error("400","更改密码错误","");
        }else {
            responseData.success("更改成功","");
        }

        return responseData.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/getUser", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String getUserByCookie(HttpServletRequest request) {

        String id = CookieUtils.isLogin(request, redisUtil);

        if ("false" == id) {
            responseData.error("400","未登录","");
            return responseData.toString();
        }
      //  return userService.getUserById(id).toString();
        User user = userService.getUserById(id);
        if (null==user){
            responseData.error("400","用户信息错误，请重新登录","");
        }else {
            responseData.success("ok", JSONObject.fromObject(user).toString());
        }


        return responseData.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/login", produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public String login(@RequestParam String userName, @RequestParam String userPasswword) {

        User user = userService.getUserByUserName(userName);

        if (null == user) {
            responseData.error("400","请检查账号","");
            return responseData.toString();
        }

        String password = MD5Util.encrypt(userName, userPasswword);
        if (password.equals(user.getUserPassword())) {
            String uuid = UUID.randomUUID().toString();             //使用随机UUID也可能存在冲突
            redisUtil.set(uuid, user.getId(), 60 * 20);           //过期时间 20m
            //Cookie cookie = new Cookie("UUID",uuid);              //统一安装跨域请求处理

            JSONObject result = new JSONObject();           //统一安装跨域请求处理
            result.put("UUID",uuid);

            responseData.success("登陆成功",result.toString());
        }else {
            responseData.error("400","请检查账号或密码","");
        }

        return responseData.toString();
    }

    @ResponseBody
    @RequestMapping(value = "/register",produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String register(@RequestBody UserRegisterDto dto) {

        if (null != userService.getUserByUserName(dto.getUserName())) {   //判断用户名是否重复
            responseData.error("400","用户名已占用","");
            return responseData.toString();
        }
        User user = new User();
        user.setUserName(dto.getUserName());
        user.setUserPassword(MD5Util.encrypt(dto.getUserName(), dto.getUserPassword()));
        user.setUserPhone(dto.getUserPhone());

        if (null == userService.insertUser(user)){
            responseData.error("400","注册错误，请重试","");
        }else {
            responseData.success("注册成功","");
        }



        return responseData.toString();
    }


    //仅仅是测试修改，真实的充值需要调用第三方充值接口。
    @ResponseBody
    @RequestMapping(value = "/recharge",produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String Recharge(HttpServletRequest request, @RequestParam int userCoin) {

        String id = CookieUtils.isLogin(request, redisUtil);
        if ("false" == id) {
            responseData.error("400","未登录","");
            return responseData.toString();
        }
        User user = userService.getUserById(id);
        user.setUserCoin(userCoin);
        if (null == userService.updateUserCoin(user)){
            responseData.error("400","充值失败","");
        }else {
            responseData.success("充值成功,充值金额："+userCoin+" RMB","");
        }

        return responseData.toString();
    }


    @ResponseBody
    @RequestMapping(value = "/buyVIP", produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    public String buyVIP(HttpServletRequest request, @RequestParam int month) {

        String id = CookieUtils.isLogin(request, redisUtil);
        if ("false" == id) {
            responseData.error("400","未登录","");
            return responseData.toString();
        }
        User user = userService.getUserById(id);

        if (null == userService.updateUserCoin(user)&&user.getUserCoin()<=150*month){
            responseData.error("400","购买失败,请重新登陆或检查金额","");
        }else {

            user = userService.getVIP(user,month);
            if (null==user){
                responseData.error("400","购买失败,请重新登陆或检查金额","");
            }else {
                responseData.success("购买"+month+"个月会员成功","");
            }
        }
        return responseData.toString();
    }




}
